package cn.netty.exemple7;

import cn.netty.exemple7.protobuf.SubscribeRepsProto;
import cn.netty.exemple7.protobuf.SubscribeReqProto;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

@ChannelHandler.Sharable
public class ProtoServerHandel extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

            SubscribeReqProto.SubscribeReq message = (SubscribeReqProto.SubscribeReq) msg;
            ctx.writeAndFlush(resp(message.getAddress()));
    }


    private SubscribeRepsProto.SubscribeReps resp(String userNmae){
        SubscribeRepsProto.SubscribeReps.Builder builder = SubscribeRepsProto.SubscribeReps.newBuilder();
        builder.setReqID(1);
        builder.setRespCode(200);
        builder.setDesc(userNmae+"3 day later");
        return builder.build();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("server is error :"+cause.getMessage());
        ctx.close();
    }
}
